package com.ncp.model.biz.article;

import java.sql.Connection;
import com.ncp.model.dao.*;
import com.ncp.model.entity.article.*;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ArticleManager {
	private Connection ct=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	private int rowCount=0;
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getRowCount() {
		return rowCount;
	}
	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	//添加文章
	public boolean addArticle(String articleTitle,String articleCont,String articleWriter,String type){
		boolean b=false;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="insert into ARTICLE values(null,'"+articleTitle+"',now(),'"+articleCont+"',0,'"+articleWriter+"','"+type+"')";
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
			b=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	//得到所有文章
	public ArrayList<ArticleBean> getAllArticles(String type){
		ArrayList<ArticleBean> al=new ArrayList<ArticleBean>();
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from ARTICLE where ArticleType='"+type+"'";
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				ArticleBean ab=new ArticleBean();
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				ab.setpDate(rs.getTimestamp(3));
				ab.setWriter(rs.getString(6));
				ab.setViews(rs.getInt(5));
				ab.setType(rs.getString(7));
				al.add(ab);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//取出一篇文章
	public ArticleBean getArticle(String id){
		ArticleBean ab=new ArticleBean();
		int views = 0;
		try {
			ct = DbConnection.getConnByDataSource();
			String sql="select * from ARTICLE where ArticleID="+id;
			ps=ct.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				ab.setpDate(rs.getTimestamp(3));
				ab.setWriter(rs.getString(6));
				views=rs.getInt(5);
				ab.setViews(views);
				ab.setType(rs.getString(7));
				this.viewsAdd(id, views);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return ab;
	}
	//增加浏览次数
	public void viewsAdd(String id,int views){
		try {
			ct = DbConnection.getConnByDataSource();
			views++;
			String sql="update ARTICLE set views="+views+" where ARTICLEID="+id;
			ps=ct.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
	}
	//删除文章
	public boolean delArticle(String id){
		Boolean b=false;
		try{
			ct = DbConnection.getConnByDataSource();
			String sql="delete from ARTICLE where ArticleID="+id;
			ps=ct.prepareStatement(sql);
			int num=ps.executeUpdate();
			if(num==1){
				b=true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return b;
	}
	//切割函数
	public static String[] split(String str, String sign) {
	  String[] strData = null;
	  StringTokenizer st1 = new StringTokenizer(str, sign);
	  strData = new String[st1.countTokens()];
	  int i = 0;
	  while (st1.hasMoreTokens()) {
	   strData[i] = st1.nextToken().trim();
	   i++;
	  }
		  return strData;
	}
	//分页
	public ArrayList fenYe(int pageSize,int pageNow){
		ArrayList al=new ArrayList();
  		try {
  			ct = DbConnection.getConnByDataSource();
  			ps=ct.prepareStatement("select count(*) from ARTICLE where ArticleType='knowledge'");
			rs=ps.executeQuery();
			if(rs.next()){
				rowCount=rs.getInt(1);	
			}
			if(rowCount%pageSize==0){
				pageCount=rowCount/pageSize;
			}else{
				pageCount=rowCount/pageSize+1;
			}
  			if(rs.next()){
  				rowCount=rs.getInt(1);
  			}
  			if(rowCount%pageSize==0){
  				pageCount=rowCount/pageSize;
  			}else{
  				pageCount=rowCount/pageSize+1;
  			}
  			ps=ct.prepareStatement("select * from ARTICLE LIMIT "+(pageNow-1)*pageSize+",10");
  			rs=ps.executeQuery();
  			while(rs.next()){
  				ArticleBean ab=new ArticleBean();
				ab.setCont(rs.getString(4));
				ab.setId(rs.getInt(1));
				ab.setTitle(rs.getString(2));
				ab.setpDate(rs.getTimestamp(3));
				ab.setPicture(rs.getString(5));
				al.add(ab);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return al;
	}
	//关闭资源
	public void closeResource(){
		try {
			if(rs!=null){
				rs.close();
				rs=null;
			}
			if(ps!=null){
				ps.close();
				ps=null;
			}
			if(ct!=null){
				ct.close();
				ct=null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
